const hosemosule = require('../../modle/hose')
const hoselovemodule = require('../../modle/userlove')
const lookhismodule = require('../../modle/lookhis')
const { Op } = require("sequelize")
module.exports = {
    async hoseadd(data) {
        const rudate = await hosemosule.create(data)
        return rudate
    },
    async findhoseaddress({ address, type, sum, area, price, limit, offset }) {
        let rudata
        if (price) {
            if (price == 1) {
                return await hosemosule.findAll({
                    where: {
                        address,
                        type,
                        sum,
                        ishas: 1
                    },
                    order: [
                        ['price']
                    ],
                    limit,
                    offset: limit * (offset - 1)
                })
            } else {
                return await hosemosule.findAll({
                    where: {
                        address,
                        type,
                        sum,
                        ishas: 1

                    },
                    order: [
                        ['price', 'DESC']
                    ],
                    limit,
                    offset: limit * (offset - 1)
                })
            }
            return rudata
        }
        if (area) {
            if (area == 1) {
                console.log(area)
                return await hosemosule.findAll({
                    where: {
                        address,
                        type,
                        sum,
                        ishas: 1

                    },
                    order: [
                        ['erea']
                    ],
                    limit,
                    offset: limit * (offset - 1)
                })
            } else {
                return await hosemosule.findAll({
                    where: {
                        address,
                        type,
                        sum,
                        ishas: 1

                    },
                    order: [
                        ['erea', 'DESC']
                    ],
                    limit,
                    offset: limit * (offset - 1)
                })
            }
            return
        }
        rudate = await hosemosule.findAll({
            where: {
                address,
                type,
                sum,
                ishas: 1
            },
            limit,
            offset: limit * (offset - 1)
        })
        return rudate
    },
    async hoseseachkey(keyword, limit, offset) {
        if (keyword == "所有") {
            return await hosemosule.findAll({
                where: {
                    ishas: 1

                },
                limit,
                offset: limit * (offset - 1)
            })
        }
        return await hosemosule.findAll({
            where: {
                address: {
                    [Op.like]: "%" + keyword + "%"
                },
                ishas: 1
            },
            limit,
            offset: limit * (offset - 1)
        })
    },
    async hosefindid(id) {
        return await hosemosule.findOne({
            where: {
                id
            }
        })
    },
    async hoselove(userid, hoseid) {
        return await hoselovemodule.create({ userid, hoseid })
    },
    async allhoselove(userid) {
        return await hoselovemodule.findAll({
            where: { userid },
            order: [
                ["createdAt", "DESC"],
            ]
        })
    },
    async leavehoselove(hoseid, userid) {
        return await hoselovemodule.destroy({
            where: {
                hoseid,
                userid
            }
        })
    },
    async historyset(hoseid, userid) {
        return await lookhismodule.create({ hoseid, userid })
    },
    async findhistroy(hoseid, userid) {
        return await lookhismodule.findOne({
            where: {
                hoseid, userid
            }
        })
    },
    async deletehistroy(id) {
        return await lookhismodule.destroy({
            where: {
                id
            }
        })
    },
    async allhistorys(userid, limit, offset) {
        return await lookhismodule.findAll({
            where: {
                userid
            },
            order: [
                ["createdAt", "DESC"],
            ],
            limit,
            offset: limit * (offset - 1)
        })
    },
    async findmyhose(userid) {
        console.log(userid)
        return await hosemosule.findAll({
            where: {
                userid
            },
            order: [
                ["updatedAt", "DESC"],
            ]
        })
    },
    async updatahoseishas(userid, hoseid, staus) {
        return await hosemosule.update({ ishas: staus }, {
            where: {
                userid,
                id: hoseid
            }
        })
    },
    async usersum(userid) {
        const hosesum = await hosemosule.findAll({
            where: {
                userid
            }
        })
        const lovesum = await hoselovemodule.findAll({
            where: {
                userid
            }
        })
        const looksum = await lookhismodule.findAll({
            where: {
                userid
            }
        })
        return { hosesum: hosesum.length, lovesum: lovesum.length, looksum: looksum.length }
    }
}